#pragma once

#include "LockGuard.hpp"
#include "Thread.hpp"
// #include "Task.hpp"
#include <queue>
#include <iostream>
#include <pthread.h>
#include <vector>

#define defaultnum 5

template <typename T>
class ThreadPool
{
private:
    void LockQueue()
    {
        pthread_mutex_lock(&_mutex);
    }
    void UnLockQueue()
    {
        pthread_mutex_unlock(&_mutex);
    }

    bool IsEmpty()
    {
        return _task_queue.empty();
    }

    void WakeUp()
    {
        pthread_cond_signal(&_cond);
    }

    void WakeUpAll()
    {
        pthread_cond_broadcast(&_cond);
    }

    void Sleep()
    {
        pthread_cond_wait(&_cond, &_mutex);
    }

    void HandlerTask(const std::string &name)
    {
        while (1)
        {
            LockQueue();
            while (IsEmpty() && _isrunning)
            {
                _sleep_thread_num++;
                Sleep();
                _sleep_thread_num--;
            }

            if (IsEmpty() && !_isrunning)
            {
                UnLockQueue();
                break;
            }

            T t;
            t = _task_queue.front();
            _task_queue.pop();
            UnLockQueue();

            t();
        }
    }

    void Init()
    {
        func_t func = std::bind(&ThreadPool::HandlerTask, this, std::placeholders::_1);
        for (int i = 0; i < _thread_num; i++)
        {
            std::string threadname = "thread - " + std::to_string(1 + i);
            _threads.emplace_back(threadname, func);
        }
    }

    void Start()
    {
        _isrunning = 1;
        for (auto &thread : _threads)
        {
            thread.start();
        }
    }

public:
    static ThreadPool<T> *GetInstance()
    {
        if (_tp == nullptr)
        {
            LockGuard lock(&_sig_mutex);
            if (_tp == nullptr)
            {
                _tp = new ThreadPool(1);
                _tp->Init();
                _tp->Start();
            }
        }
        return _tp;
    }

    void Equeue(const T &in)
    {
        LockQueue();
        if (_isrunning)
        {
            _task_queue.push(in);
            if (_sleep_thread_num > 0)
                WakeUp();
        }
        UnLockQueue();
    }

    void Stop()
    {
        LockQueue();
        _isrunning = 0;
        WakeUpAll();
        UnLockQueue();
    }

private:
    ThreadPool(int thread_num = defaultnum)
        : _thread_num(thread_num), _isrunning(0), _sleep_thread_num(0)
    {
        pthread_mutex_init(&_mutex, nullptr);
        pthread_cond_init(&_cond, nullptr);
    }
    // ThreadPool() = delete;
    ThreadPool(const ThreadPool<T> &) = delete;
    ThreadPool<T> &operator=(const ThreadPool<T> &) = delete;

    bool _isrunning;
    int _thread_num;
    std::vector<Thread> _threads;
    std::queue<T> _task_queue;

    pthread_mutex_t _mutex;
    pthread_cond_t _cond;

    int _sleep_thread_num;

    static ThreadPool<T> *_tp;
    static pthread_mutex_t _sig_mutex;
};

template <typename T>
ThreadPool<T> *ThreadPool<T>::_tp = nullptr;

template <typename T>
pthread_mutex_t ThreadPool<T>::_sig_mutex = PTHREAD_MUTEX_INITIALIZER;